Skip to content

plugin

Pytest Plugin.

Classes⚓︎

TestMetadata ⚓︎

Bases: BaseModel

Test MetaData.

Source code in pytest_cache_assert/plugin.py
class TestMetadata(BaseModel):
    """Test MetaData."""

    test_file: str
    test_name: str
    func_args: Union[Dict, Iterable]  # type: ignore[type-arg]
    model_config = ConfigDict(arbitrary_types_allowed=True, frozen=True)

    @classmethod
    @beartype
    def from_pytest(cls, request: FixtureRequest, rel_test_file: Path) -> TestMetadata:
        """Resolve TestMetadata.

        Args:
            request: pytest fixture used to identify the test directory
            rel_test_file: relative path to the test file

        Returns:
            TestMetadata: new TestMetadata

        """
        test_name = request.node.originalname  # pyright: ignore[reportGeneralTypeIssues]
        function = request.node.function  # pyright: ignore[reportGeneralTypeIssues]
        func_arg_name_value_pairs = request.node.funcargs.items()  # pyright: ignore[reportGeneralTypeIssues]

        full_test_name = (f'{request.cls.__name__}/' if request.cls else '') + test_name
        test_params = [*inspect.signature(function).parameters.keys()]
        func_args = {key: value for key, value in func_arg_name_value_pairs if key in test_params}
        return cls(test_file=rel_test_file.as_posix(), test_name=full_test_name, func_args=func_args)

Functions⚓︎

from_pytest classmethod ⚓︎
from_pytest(request, rel_test_file)

Resolve TestMetadata.

PARAMETER DESCRIPTION
request

pytest fixture used to identify the test directory

TYPE: FixtureRequest

rel_test_file

relative path to the test file

TYPE: Path

RETURNS DESCRIPTION
TestMetadata

new TestMetadata

TYPE: TestMetadata

Source code in pytest_cache_assert/plugin.py
@classmethod
@beartype
def from_pytest(cls, request: FixtureRequest, rel_test_file: Path) -> TestMetadata:
    """Resolve TestMetadata.

    Args:
        request: pytest fixture used to identify the test directory
        rel_test_file: relative path to the test file

    Returns:
        TestMetadata: new TestMetadata

    """
    test_name = request.node.originalname  # pyright: ignore[reportGeneralTypeIssues]
    function = request.node.function  # pyright: ignore[reportGeneralTypeIssues]
    func_arg_name_value_pairs = request.node.funcargs.items()  # pyright: ignore[reportGeneralTypeIssues]

    full_test_name = (f'{request.cls.__name__}/' if request.cls else '') + test_name
    test_params = [*inspect.signature(function).parameters.keys()]
    func_args = {key: value for key, value in func_arg_name_value_pairs if key in test_params}
    return cls(test_file=rel_test_file.as_posix(), test_name=full_test_name, func_args=func_args)

Functions⚓︎

assert_against_cache ⚓︎

assert_against_cache(request, cache_assert_config=None)

Return main.assert_against_cache with pytest-specific arguments already specified.

PARAMETER DESCRIPTION
request

pytest fixture used to identify the test directory

TYPE: FixtureRequest

cache_assert_config

pytest fixture that returns AssertConfig for user configuration

TYPE: Optional[AssertConfig] DEFAULT: None

RETURNS DESCRIPTION
Callable[[Any], None]

Callable[[Any], None]: main.assert_against_cache() with test_dir already specified

RAISES DESCRIPTION
RuntimeError

if the test directory cannot be determined

Source code in pytest_cache_assert/plugin.py
@pytest.fixture()
@beartype
def assert_against_cache(
    request: FixtureRequest,
    cache_assert_config: Optional[AssertConfig] = None,
) -> Callable[[Any], None]:
    """Return main.assert_against_cache with pytest-specific arguments already specified.

    Args:
        request: pytest fixture used to identify the test directory
        cache_assert_config: pytest fixture that returns AssertConfig for user configuration

    Returns:
        Callable[[Any], None]: `main.assert_against_cache()` with test_dir already specified

    Raises:
        RuntimeError: if the test directory cannot be determined

    """
    path_cache_dir, rel_test_file, cache_name = _inner_plugin(request, cache_assert_config)
    metadata = TestMetadata.from_pytest(request=request, rel_test_file=rel_test_file).dict()

    # FYI: The partial function keyword arguments can be overridden when called
    return partial(main.assert_against_cache, path_cache_dir=path_cache_dir, cache_name=cache_name, metadata=metadata)

read_from_cache ⚓︎

read_from_cache(request, cache_assert_config=None)

Return main.assert_against_cache with pytest-specific arguments already specified.

PARAMETER DESCRIPTION
request

pytest fixture used to identify the test directory

TYPE: FixtureRequest

cache_assert_config

pytest fixture that returns AssertConfig for user configuration

TYPE: Optional[AssertConfig] DEFAULT: None

RETURNS DESCRIPTION
Callable[[], Any]

Callable[[Any], None]: main.assert_against_cache() with test_dir already specified

RAISES DESCRIPTION
RuntimeError

if the test directory cannot be determined

Source code in pytest_cache_assert/plugin.py
@pytest.fixture()
@beartype
def read_from_cache(
    request: FixtureRequest,
    cache_assert_config: Optional[AssertConfig] = None,
) -> Callable[[], Any]:
    """Return main.assert_against_cache with pytest-specific arguments already specified.

    Args:
        request: pytest fixture used to identify the test directory
        cache_assert_config: pytest fixture that returns AssertConfig for user configuration

    Returns:
        Callable[[Any], None]: `main.assert_against_cache()` with test_dir already specified

    Raises:
        RuntimeError: if the test directory cannot be determined

    """
    path_cache_dir, _rel_test_file, cache_name = _inner_plugin(request, cache_assert_config)

    # FYI: The partial function keyword arguments can be overridden when called
    return partial(main.read_from_cache, path_cache_dir=path_cache_dir, cache_name=cache_name)

Last update: August 30, 2023
Created: August 30, 2023